﻿using System;
using System.Collections.Generic;
using System.Text;
using wojilu.Apps.Qycms.Domain;
using wojilu.Members.Interface;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Sites.Service;
using wojilu.Members.Users.Domain;
using wojilu.Web.Controller.Common;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller.Admin.Qycms
{
    public class SEOController : BaseController
    {
        public virtual IAdminLogService<SiteLog> logService { get; set; }

        public SEOController()
        {
            logService = new SiteLogService();
        }

        private void setpublic()
        {
            User user = ctx.viewer.obj as User;
            var modelPowerGroup = PowerGroup.findById(user.GroupId);
            bind("powergroup", modelPowerGroup);
        }

        [HttpGet]
        public virtual void SEOSelect()
        {
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("lng", DefaultLng);
            set("inputtext", ctx.Get("inputtext"));
            set("listfunction", ctx.Get("listfunction"));
            set("checkfrom", ctx.Get("checkfrom"));
            set("digheight", ctx.GetInt("digheight"));
            set("getbyid", ctx.Get("getbyid"));
            var mid = ctx.GetInt("mid");
            set("mid", mid);
            set("tid", ctx.GetInt("tid"));
            var list1 = Model.findAll();
            bind("list", list1);
            var condition = " lng='" + DefaultLng + "' and upid = 0 ";
            if (mid > 0)
            {
                condition += " and mid = " + mid;
            }
            condition += " order by pid asc,id asc";
            var list = wojilu.Apps.Qycms.Domain.TypeList.find(condition).list();
            var list2 = new List<TypeList>();
            foreach (var row in list)
            {
                list2.Add(row);
                type_list(ref list2, row);
            }
            set("list1", list2);
        }

        [HttpGet, HttpPost]
        public virtual void SeoLinkList()
        {
            DataPage<KeyLink> list = null;
            var mid = ctx.GetInt("mid");
            var lng = ctx.Get("lng");
            var tid = ctx.GetInt("tid");
            var isclass = ctx.GetInt("isclass");
            var listtype = ctx.Get("listtype");
            var serchekey = ctx.Get("serchekey");
            var MinPageid = ctx.GetInt("MinPageid");
            var page_id = ctx.GetInt("page_id");
            var MaxPerPage = ctx.GetInt("MaxPerPage");
            var condition = " lng='" + lng + "' and isclass=" + isclass;
            if (mid > 0)
            {
                condition += " and mid=" + mid;
            }
            if (tid > 0)
            {
                condition += " and tid in(" + GetChildStr(tid, true) + ")";
            }
            if (!serchekey.StrIsNullOrEmpty())
            {
                condition += " and keywordname like '%" + serchekey + "%'";
            }
            var limitkey = "pid";
            var limitclass = "asc";
            condition += " order by " + limitkey + " " + limitclass + ",id desc";
            CurrentRequest.setCurrentPage(page_id);
            list = KeyLink.findPage(condition, MaxPerPage);
            bind("list", list.Results);
        }

        public string GetChildStr(Int64 tid, bool isNow)
        {
            var str = string.Empty;
            if (isNow)
            {
                str += tid + ",";
            }
            GetChildStr(tid, ref str);
            if (str.EndsWith(","))
            {
                str = str.Remove(str.Length - 1, 1);
            }
            return str;
        }

        private void GetChildStr(Int64 tid, ref string str)
        {
            var list = TypeList.find(" upid =:u").set("u", tid).list(); 
            foreach (var row in list)
            {
                str += row.Id + ",";
                GetChildStr(row.Id, ref str);
            }
        }

        [HttpGet, HttpPost]
        public virtual void SeoLinkTypeList()
        {
            DataPage<Keylink_Type> list = null;
            var mid = ctx.GetInt("mid");
            var lng = ctx.Get("lng");
            var tid = ctx.GetInt("tid");
            var isclass = ctx.GetInt("isclass");
            var listtype = ctx.Get("listtype");
            var serchekey = ctx.Get("serchekey");
            var MinPageid = ctx.GetInt("MinPageid");
            var page_id = ctx.GetInt("page_id");
            var MaxPerPage = ctx.GetInt("MaxPerPage");
            var condition = " lng='" + lng + "' and isclass=" + isclass;
            if (mid > 0)
            {
                condition += " and mid=" + mid;
            }
            if (tid > 0)
            {
                condition += " and Id in(" + GetChildStr(tid, true) + ")";
            }
            if (!serchekey.StrIsNullOrEmpty())
            {
                condition += " and keywordname like '%" + serchekey + "%'";
            }
            condition += " order by id desc";
            CurrentRequest.setCurrentPage(page_id);
            list = Keylink_Type.findPage(condition, MaxPerPage);
            bind("list", list.Results);
        }

        [HttpGet]
        public virtual void ScTypeList()
        {
            var optiontitle = ctx.Get("optiontitle");
            var builder = new StringBuilder();
            var mid = ctx.GetInt("mid");
            builder.Append("<option value=\"0\">" + optiontitle + "</option>");
            var condition = " lng='" + ctx.Get("lng") + "' and upid = 0 ";
            if (mid > 0)
            {
                condition += " and mid = " + mid;
            }
            condition += " order by pid asc,id asc";
            var list = wojilu.Apps.Qycms.Domain.TypeList.find(condition).list();
            var list2 = new List<TypeList>();
            foreach (var row in list)
            {
                list2.Add(row);
                type_list(ref list2, row);
            }
            foreach (var row in list2)
            {
                var str = string.Empty;
                for (var i = 1; i < row.level + 1; i++)
                {
                    str += "&nbsp;&nbsp;&nbsp;&nbsp;";
                }
                if (row.level >= 0)
                {
                    str += "├─";
                }
                builder.Append("<option value=\"" + row.Id + "\">" + str + row.typename + "</option>");
            }
            ctx.web.ResponseWrite(builder.ToString());
            ctx.web.ResponseEnd();
        }

        [NonVisit]
        public void type_list(ref List<TypeList> list, TypeList row)
        {
            var condition = " lng='" + DefaultLng + "' and upid = " + row.Id;
            DataPage<TypeList> list1 = null;
            Int32 maxperpage = ctx.PostInt("MaxPerPage");
            Int32 page_id = ctx.PostInt("page_id") + 1;
            String limitkey = ctx.Get("limitkey");
            String limitclass = ctx.Get("limitclass");
            var mid = ctx.GetInt("mid");
            if (mid > 0)
            {
                condition += " and mid = " + mid;
            }
            if (strUtil.IsNullOrEmpty(limitkey)) limitkey = "pid";
            if (strUtil.IsNullOrEmpty(limitclass)) limitclass = "asc";
            condition += " order by " + limitkey + " " + limitclass + ",id asc";
            list1 = wojilu.Apps.Qycms.Domain.TypeList.findPage(condition, maxperpage);
            foreach (var row1 in list1.Results)
            {
                list.Add(row1);
                type_list(ref list, row1);
            }
        }

    }
}
